package com.iecas.utils;

import java.nio.channels.SocketChannel;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

public class SocketChannelManager {
    private static CopyOnWriteArrayList<SocketChannel> scList = new CopyOnWriteArrayList();
    private static Map<SocketChannel,String> scTokenMap =  new ConcurrentHashMap<>();

    public static void add(SocketChannel socketChannel,String token) {
        scList.add(socketChannel);
        scTokenMap.put(socketChannel,token);
    }

    public static List<SocketChannel> getSocketChannelList() {
        return scList;
    }

    public static String remove(SocketChannel socketChannel) {
         scList.remove(socketChannel);
        return scTokenMap.remove(socketChannel);
    }

    public static SocketChannel get(String token) {
        for (Map.Entry<SocketChannel, String> entry : scTokenMap.entrySet()) {
            if (entry.getValue().equals(token)) {
                return entry.getKey();
            }
        }
        return null;
    }


}
